<?php


namespace app\admin\model\shop;


use app\common\model\TimeModel;
use app\common\constants\MenuConstant;
class Category extends TimeModel
{
    protected $pk = 'cate_id';
    public function getPidMenuList()
    {
        $list        = $this->field('cate_id,pid,cate_name')
            ->where([
                ['pid', '<>', MenuConstant::HOME_PID],
            ])
            ->select()
            ->toArray();
        $pidMenuList = $this->buildPidMenu(0, $list);
        $pidMenuList = array_merge([[
            'cate_id'    => 0,
            'pid'   => 0,
            'cate_name' => '顶级分类',
        ]], $pidMenuList);
        return $pidMenuList;
    }
    public function getLevel(&$data,$pid = 0,$count = 1) {
        static $treeList=[];
         foreach ($data as $key => $value){
             if($value['pid']==$pid){
                 $value['level'] = $count;
                 $treeList []=$value;
                 // unset($data[$key]);
                 $this -> getLevel($data,$value['cate_id'],$count+1);
             }
         }
         foreach ($treeList as $k => $v) {
             $treeList[$k]['count']=$this->where('pid',$v['cate_id'])->count();
         }
         return $treeList ;
     }
    public function buildPidMenu($pid, $list, $level = 0)
    {
        $newList = [];
        foreach ($list as $vo) {
            if ($vo['pid'] == $pid) {
                $level++;
                foreach ($newList as $v) {
                    if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
                        $level = $v['level'];
                        break;
                    }
                }
                $vo['level'] = $level;
                if ($level > 1) {
                    $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    $markString   = str_repeat("{$repeatString}├{$repeatString}", $level - 1);
            
                    $vo['cate_name']  = $markString . $vo['cate_name'];
                }
                $newList[] = $vo;
                $childList = $this->buildPidMenu($vo['cate_id'], $list, $level);
                !empty($childList) && $newList = array_merge($newList, $childList);
            }

        }
        return $newList;
    }
    public function getCates(){
        $list        = $this->field('cate_id,pid,cate_name,image,type')
        ->where([
            ['status', '=', 1],
        ])
        ->select()
        ->toArray();
        $data = $this->genTree($list);
        return $data;
    }
    protected function genTree($items,$id='cate_id',$pid='pid',$son = 'children'){
		$tree = array(); //格式化的树
		$tmpMap = array();  //临时扁平数据
		
		foreach ($items as $item) {
			$tmpMap[$item[$id]] = $item;
		}
       
		foreach ($items as $item) {
           
			if (isset($tmpMap[$item[$pid]])) {
				$tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
			} else {
              
				$tree[] = &$tmpMap[$item[$id]];
			}
		}
		return $tree;
    }
      //所选店铺下的分类
     public function getCate($cate_id){
        $where['cate_id']=$cate_id;
        $list  = $this->field('cate_id,cate_name')->where($where)->find();

        return $list;
    }

}